package org.fast.develop.logicflow.runtime.monitor;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.fast.develop.logicflow.core.meta.base.Resource;
import org.fast.develop.logicflow.core.meta.shape.line.SequenceFlow;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author czh
 * @version V1.0
 * @data Created on 2023-01-04 17:04
 */
@Data
@Accessors(chain = true)
public class ExecuteLog {

    private Calendar calendar;

    private String name;

    private Resource resource;

    private String msg;

    public ExecuteLog(String name, Resource resource) {
        this.calendar = Calendar.getInstance();
        this.name = name;
        this.resource = resource;
    }

    public ExecuteLog(String name, Resource resource, String msg) {
        this.calendar = Calendar.getInstance();
        this.name = name;
        this.resource = resource;
        this.msg = msg;
    }

    public String getMsg() {
        if(StringUtils.isBlank(this.msg)){
            String elementName;
            if (resource instanceof SequenceFlow) {
                elementName = "连线";
            } else {
                elementName = "节点";
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return StrUtil.format("[{}] 执行{},{}名称为:{},{}id为:{}", sdf.format(calendar.getTime()), elementName, elementName, name, elementName, resource.getResourceId());
        } else {
            return msg;
        }
    }
}
